Package org.javacommerce.google.servlet

Source Code of org.javacommerce.google.servlet.CheckoutServlet

/**
*
*/
package org.javacommerce.google.servlet;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.javacommerce.core.config.JavaCommerce;
import org.javacommerce.google.APIUtil;
import org.javacommerce.google.ws.API;
import org.javacommerce.google.ws.APIException;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.xml.sax.SAXException;

import com.google.checkout.CheckoutRedirect;
import com.google.checkout.CheckoutShoppingCart;

/**
* @author Michael Blanton (mike@mikeblanton.com)
* @web.servlet name="Checkout"
* @web.servlet-mapping
*   url-pattern="/Checkout"
*/
public class CheckoutServlet extends HttpServlet {

  /**
   *
   */
  private static final long serialVersionUID = 8746495624866745953L;
  private static final Log LOG = LogFactory.getLog(CheckoutServlet.class);

  /* (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  protected void doPost(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
    Document xml = null;
    try {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Generating Shopping Cart");
      }
      CheckoutShoppingCart cart = APIUtil.buildCheckoutShoppingCart(_request);
      String encSig = APIUtil.generateCartSignature(cart);
      String encCart = APIUtil.encodeCart(cart);
      if (_request.getParameter(APIUtil.PARAM_REDIRECT) != null && _request.getParameter(APIUtil.PARAM_REDIRECT).equals("true")) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Posting Cart to Google");
        }
        CheckoutRedirect redirect = API.postCheckoutShoppingCart(cart);
        if (LOG.isDebugEnabled()) {
          LOG.debug("Cart posted, adding redirect URL");
        }
        xml = APIUtil.generateXMLDocument(encSig, encCart, redirect);
      }
      else {
        xml = APIUtil.generateXMLDocument(encSig, encCart);
      }
    } catch (InvalidKeyException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("InvalidKeyException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (MarshalException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("MarshalException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (ValidationException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("ValidationException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (NoSuchAlgorithmException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("NoSuchAlgorithmException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (HttpException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("HttpException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("IOException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (ParserConfigurationException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("ParserConfigurationException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (TransformerException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("TransformerException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (SAXException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("SAXException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (JDOMException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("JDOMException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    } catch (APIException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("APIException generating CheckoutShoppingCart for " + API.GOOGLE_MERCHANT_ID + ": " + e.getLocalizedMessage(), e);
      }
      xml = JavaCommerce.buildExceptionXML(e, "There was an exception processing the request - please contact your administrator.");
    }
    _response.addHeader("Content-Type", "text/xml");
    JavaCommerce.XML_COMPACT_OUTPUTTER.output(xml, _response.getOutputStream());
    if (LOG.isDebugEnabled()) {
      LOG.debug("XML written to Output Stream");
    }

  }

}
TOP

Related Classes of org.javacommerce.google.servlet.CheckoutServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.